package run.theo.mallorder.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import run.theo.mallcommons.entity.MallOrder;
import run.theo.mallcommons.entity.Product;
import run.theo.mallcommons.entity.Result;
import run.theo.mallorder.service.MallOrderService;
import run.theo.mallorder.feign.ProductsProvider;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author theo
 * @since 2022/10/16 14:38
 */
@Slf4j
@RestController()
@RequestMapping("/mallOrder")
public class MallOrderController {

    @Autowired
    private ProductsProvider productsProvider;

    @GetMapping("/prod/{pid}")
    public Result order(@PathVariable("pid") int pid){
        log.info("接收到{}号商品的下单请求，接下来调用商品微服务查询此商品信息",pid);
        Product product = productsProvider.findByPid(pid);
        log.info("查询到{}号商品的信息，内容是：{}",pid,product);

        if (product.getPid() == -1) {
            return new Result(-1,product.getPname(),null);
        }
        //下单（创建订单）
        MallOrder mallOrder = new MallOrder();
        mallOrder.setUid(1);
        mallOrder.setUsername("Theo");

        mallOrder.setPid(pid);
        mallOrder.setPname(product.getPname());
        mallOrder.setPprice(product.getPprice());
        mallOrder.setNumber(1);

//        boolean save = mallOrderService.save(mallOrder);
        log.info("创建订单成功，订单信息为{}",44);
        return Result.updateResp(1,"订单创建成功","订单创建失败",mallOrder);
    }
}
